<template>
  <section v-loading="loading">
    <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
    <div style="margin: 15px 0;"></div>
    <el-checkbox-group v-model="checkedExamineType" @change="handleCheckedExamineTypeChange">
      <el-checkbox v-for="examineType in examineTypes" :label="examineType" :key="examineType.code">
        {{examineType.name}}
      </el-checkbox>
    </el-checkbox-group>
    <el-row>
      <el-button @click="subBtn" size="mini" type="primary" :disabled="checkedExamineType.length>0?false:true">提交生成</el-button>
    </el-row>
  </section>
</template>

<script>
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  export default {
    name: "checkItems",
    props: ['currAppointment','job'],
    data(){
      return {
        loading:false,
        checkAll: false,
        checkedExamineType: [],
        examineTypes:null,
        isIndeterminate: true
      }
    },
    computed:{
      ...mapGetters(['department']),
    },
    mounted(){
    },
    created(){
      console.log(this.job)
      //获取体检类型列表
      this.getExamineTypeList()
    },
    watch:{
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      judeg(key){
        let pattern = new RegExp("[\u4E00-\u9FA5]+");
        let str = key
        if(pattern.test(str)){
          return true
        }
      },
      getExamineTypeList(){//获取体检类型列表
        this.fetch({name:"getExamineTypeList"})
          .then(res=>{
            console.log(res)
            this.examineTypes=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      handleCheckAllChange(val) {//控制全选
        this.checkedExamineType = val ? this.examineTypes : [];
        this.isIndeterminate = false;
      },
      handleCheckedExamineTypeChange(value) {//控制单个checkbox
        let checkedCount = value.length
        this.checkAll = checkedCount === this.examineTypes.length;
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.examineTypes.length;
      },
      subBtn(){
        this.loading=true
        if(this.currAppointment === undefined){
          //自己创建体检，组装参数
          this.makeExamineData()
          return
        }
        //组装参数  'name','appointmentId']//data,examineItems
        let name=''+this.currAppointment.userName
        for(let key in this.currAppointment.data){
          if(this.judeg(key)){
           let n = this.currAppointment.data[key]
           name += '-'+n
          }
        }
        for(let key2 in this.currAppointment.orderItem.data){
          if(this.judeg(key2)){
            let n = this.currAppointment.orderItem.data[key2]
            name += '-'+n
          }
        }
        console.log(name)
        let examineItems=[]
        this.checkedExamineType.forEach(e=>{
          let examineItem={
            examineTypeId:e.id,
            name:e.name
          }
          examineItems.push(examineItem)
        })
        let data={
          name,
          appointmentId:this.currAppointment.id,
          examineItems
        }
        console.log(data)
        this.createExamineByAppointment(data)
      },
      createExamineByAppointment(data) {
        this.fetch({name:"createExamineByAppointment",data})
          .then(res=>{
            this.loading=false
            this.$message({type:'success',message:'体检创建成功'})
            this.$emit('cancel')
            console.log(res)
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      makeExamineData(){
        console.log(this.checkedExamineType)
        // 'name','departmentCode','jobId']//data(选)、、examineItems(选)
        let data={name:'job-'+this.job.mark,jobId:this.job.id,departmentCode:this.department.code}
        let examineItems=[]
        this.checkedExamineType.forEach(e=>{
          let examineItem={
            examineTypeId:e.id,
            name:e.name
          }
          examineItems.push(examineItem)
        })
        data.examineItems=JSON.stringify(examineItems)
        console.log(data)
        this.createExamine(data)
      },
      createExamine(data) {
        console.log(data)
        this.fetch({name:"createExamine",data})
          .then(res=>{
            this.loading=false
            console.log(res)
            this.$message({type:'success',message:'体检创建成功'})
            this.$emit('cancel')
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
    }
  }
</script>

<style scoped>

</style>
